<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Items extends MY_Controller
{
	private $can_do;

	public function __construct()
	{
		parent::__construct();
		$this->can_do = ' and (begin<=' . time() . ' and end>' . time() . ') ';
	}

	public function index()
	{
		$flag = intval($this->input->get('flag'));
		$where = '(status=2 or status=7 or status=3)';

		if ($flag == 1) {
			$where .= ' and begin>' . time();
		}
		else if ($flag == 2) {
			$where .= ' and (begin<' . time() . ' and end>' . time() . ' and status<>3)';
		}
		else if ($flag == 3) {
			$where .= ' and (end<' . time() . ' or status<>3)';
		}

		$d = $this->result_list(array('table' => 'items', 'where' => $where, 'order' => 'add_time desc', 'url' => 'items/index', 'per_page' => 20));

		foreach ($d['list'] as $k => $v) {
			$d['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
			$d['list'][$k]['percent'] = 0 < $v['target_amount'] ? num(($v['deal'] / $v['target_amount']) * 100, 4) : 0;
			$d['list'][$k]['name'] = mb_substr($v['name'], 0, 12, 'utf-8');
			$d['list'][$k]['intro'] = mb_substr($v['intro'], 0, 70, 'utf-8');
			$d['list'][$k]['target_amount'] = num($v['target_amount']);
			$d['list'][$k]['deal'] = num($v['deal']);
			$d['list'][$k]['day'] = $v['begin'] < $v['end'] ? intval(($v['end'] - $v['begin']) / (24 * 3600)) : 0;

			if (time() < $v['begin']) {
				$d['list'][$k]['stat'] = 0;
			}
			else {
				if (($v['begin'] < time()) && (time() < $v['end'])) {
					if ($v['is_over'] == 1) {
						$d['list'][$k]['stat'] = 1;
						$d['list'][$k]['day'] = time() < $v['end'] ? intval(($v['end'] - time()) / (24 * 3600)) : 0;
					}
					else if ($v['target_amount'] <= $v['deal']) {
						$d['list'][$k]['stat'] = 2;
						$d['list'][$k]['day'] = 0;
					}
					else {
						$d['list'][$k]['stat'] = 1;
						$d['list'][$k]['day'] = time() < $v['end'] ? intval(($v['end'] - time()) / (24 * 3600)) : 0;
					}
				}
				else {
					$d['list'][$k]['stat'] = 2;
				}
			}
		}

		$d['flag'] = $flag;
		$this->show(array('header', 'items', 'footer'), $d);
	}

	public function info()
	{
		$id = intval($this->input->get('id'));

		if (!$id) {
			error_404();
		}

		$d = $this->d->find(array('table' => 'items', 'where' => 'id=' . $id . ' and (status=2 or status=7 or status=3)'));

		if (!$d) {
			error_404();
		}

		$d['day'] = $d['begin'] < $d['end'] ? intval(($d['end'] - $d['begin']) / (24 * 3600)) : 0;

		if (time() < $d['begin']) {
			$d['stat'] = 0;
		}
		else {
			if (($d['begin'] < time()) && (time() < $d['end'])) {
				if ($d['is_over'] == 1) {
					$d['stat'] = 1;
					$d['day'] = time() < $d['end'] ? intval(($d['end'] - time()) / (24 * 3600)) : 0;
				}
				else if ($d['target_amount'] <= $d['deal']) {
					$d['stat'] = 2;
					$d['day'] = 0;
				}
				else {
					$d['stat'] = 1;
					$d['day'] = time() < $d['end'] ? intval(($d['end'] - time()) / (24 * 3600)) : 0;
				}
			}
			else {
				$d['stat'] = 2;
			}
		}

		$d['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $d['coin_id']));
		$d['percent'] = 0 < $d['target_amount'] ? num(($d['deal'] / $d['target_amount']) * 100, 4) : 0;
		$d['ower'] = $this->d->find(array('table' => 'user', 'where' => 'id=' . $d['user_id']));
		$d['deal'] = num($d['deal']);
		$d['target_amount'] = num($d['target_amount']);

		if ($d['is_lock'] == 1) {
			$lock = $this->d->find(array('table' => 'support', 'where' => 'is_lock=1 and item_id=' . $id . ' and user_id=' . $this->user['id']));

			if ($lock) {
				$d['is_supported'] = 1;
			}
			else {
				$d['is_supported'] = 0;
			}
		}

		$this->show(array('header', 'items_first', 'items_view', 'items_last', 'footer'), $d);
	}

	public function progress()
	{
		$id = intval($this->input->get('id'));

		if (!$id) {
			error_404();
		}

		$d = $this->d->find(array('table' => 'items', 'where' => 'id=' . $id . ' and (status=2 or status=7 or status=3)'));

		if (!$d) {
			error_404();
		}

		if (time() < $d['begin']) {
			$d['stat'] = 0;
		}
		else {
			if (($d['begin'] < time()) && (time() < $d['end'])) {
				$d['stat'] = 1;
			}
			else {
				$d['stat'] = 2;
			}
		}

		$d['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $d['coin_id']));
		$d['percent'] = 0 < $d['target_amount'] ? num(($d['deal'] / $d['target_amount']) * 100) : 0;
		$d['ower'] = $this->d->find(array('table' => 'user', 'where' => 'id=' . $d['user_id']));
		$this->show(array('header', 'items_first', 'items_progress', 'items_last', 'footer'), $d);
	}

	public function support()
	{
		$id = intval($this->input->get('id'));

		if (!$id) {
			error_404();
		}

		$d = $this->d->find(array('table' => 'items', 'where' => 'id=' . $id . ' and (status=2 or status=7 or status=3)'));

		if (!$d) {
			error_404();
		}

		if (time() < $d['begin']) {
			$d['stat'] = 0;
		}
		else {
			if (($d['begin'] < time()) && (time() < $d['end'])) {
				$d['stat'] = 1;
			}
			else {
				$d['stat'] = 2;
			}
		}

		$d['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $d['coin_id']));
		$d['percent'] = 0 < $d['target_amount'] ? num(($d['deal'] / $d['target_amount']) * 100) : 0;
		$d['ower'] = $this->d->find(array('table' => 'user', 'where' => 'id=' . $d['user_id']));
		$rows = $this->result_list(array('table' => 'support', 'where' => 'item_id=' . $id, 'order' => 'add_time desc', 'url' => 'items/support', 'per_page' => 20));

		foreach ($rows['list'] as $k => $v) {
			$rows['list'][$k]['user'] = $this->d->find(array('table' => 'user', 'where' => 'id=' . $v['user_id']));
			$rows['list'][$k]['user']['username'] = substr($rows['list'][$k]['user']['username'], 0, 1) . '*****' . substr($rows['list'][$k]['user']['username'], -1, 1);
			$rows['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
		}

		$d['list'] = $rows['list'];
		$d['page'] = $rows['page'];
		$this->show(array('header', 'items_first', 'items_support', 'items_last', 'footer'), $d);
	}

	public function discuss()
	{
		$id = intval($this->input->get('id'));

		if (!$id) {
			error_404();
		}

		$d = $this->d->find(array('table' => 'items', 'where' => 'id=' . $id . ' and (status=2 or status=7 or status=3)'));

		if (!$d) {
			error_404();
		}

		if (time() < $d['begin']) {
			$d['stat'] = 0;
		}
		else {
			if (($d['begin'] < time()) && (time() < $d['end'])) {
				$d['stat'] = 1;
			}
			else {
				$d['stat'] = 2;
			}
		}

		$d['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $d['coin_id']));
		$d['percent'] = 0 < $d['target_amount'] ? num(($d['deal'] / $d['target_amount']) * 100) : 0;
		$d['ower'] = $this->d->find(array('table' => 'user', 'where' => 'id=' . $d['user_id']));
		$rows = $this->result_list(array('table' => 'discuss', 'where' => 'item_id=' . $id, 'order' => 'add_time desc', 'url' => 'items/discuss', 'per_page' => 20));

		foreach ($rows['list'] as $k => $v) {
			$rows['list'][$k]['user'] = $this->d->find(array('table' => 'user', 'where' => 'id=' . $v['user_id']));
			$rows['list'][$k]['user']['username'] = substr($rows['list'][$k]['user']['username'], 0, 1) . '*****' . substr($rows['list'][$k]['user']['username'], -1, 1);

			if ($v['reply_id']) {
				$rows['list'][$k]['reply'] = $this->d->find(array('table' => 'discuss', 'where' => 'id=' . $v['reply_id']));
				$rows['list'][$k]['reply']['user'] = $this->d->find(array('table' => 'user', 'where' => 'id=' . $rows['list'][$k]['reply']['user_id']));
				$rows['list'][$k]['reply']['user']['username'] = substr($rows['list'][$k]['reply']['user']['username'], 0, 1) . '*****' . substr($rows['list'][$k]['reply']['user']['username'], -1, 1);
			}
		}

		$d['list'] = $rows['list'];
		$d['page'] = $rows['page'];
		$this->show(array('header', 'items_first', 'items_discuss', 'items_last', 'footer'), $d);
	}

	public function discuss_insert()
	{
		$this->verify_user_ajax();
		$reply_id = intval($this->input->post('reply_id'));
		$item_id = intval($this->input->post('item_id'));
		$content = $this->input->post('content');

		if (!$item_id) {
			ajax_return(1, '找不到项目');
		}

		if (!$content) {
			ajax_return(1, '请输入内容');
		}

		$d = $this->d->find(array('table' => 'items', 'where' => 'id=' . $item_id . ' and status=2' . $this->can_do));

		if (!$d) {
			ajax_return(1, '找不到项目');
		}

		$data = array('reply_id' => $reply_id, 'item_id' => $item_id, 'content' => $content, 'user_id' => $this->user['id'], 'add_time' => time());

		if ($this->db->insert('discuss', $data)) {
			ajax_return(0, '提交成功');
		}
		else {
			ajax_return(1, '提交失败');
		}
	}
}

?>
